home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5590 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: gryphon.phoenix.net!usenet
  2. From: brucew@phoenix.net (Bruce Wedding)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C beginner needs your help ASAP
  5. Date: Tue, 20 Feb 1996 01:25:14 GMT
  6. Organization: BranPaul Systems
  7. Message-ID: <4gb7uv$cr2@gryphon.phoenix.net>
  8. References: <4g862f$p0b@risky.ecs.umass.edu>
  9. NNTP-Posting-Host: dial147.phoenix.net
  10. X-Newsreader: Moe's Newsreader    
  11.  
  12.  
  13. >I am a new C programmmer who desperately needs help.
  14. >I have been digging in the manuals for a way to do this but have still
  15. >come out empty handed. Here is the problem: I want to open files in a while
  16. >loop with different filenames. data0,data1,data2,data3, .. data1000 , ...
  17. >I need to increment an integer each time around then convert it to a string
  18. >and then somehow use strcat to combine the "data" with the integer string.
  19. >After that use fopen(filename, "a");
  20.  
  21. I'm guessing you have an OS that allows you to open 1000 files?
  22.  
  23. FILE *fp[1000];
  24. char buf[80];
  25.  
  26. for ( i =0; i < 1000; i++)
  27. {
  28.     sprintf(buf, "data%d", i);
  29.     fp[i] = fopen(buf, "w");
  30.     if (fp[i] == NULL)
  31.         /* error message */
  32. }
  33.  
  34. That should do it.
  35.  
  36. Bruce D. Wedding                        Have Compiler, Will Travel!
  37.               Perspicacious Programming Performed Promptly
  38. Katy, Texas, USA, Planet Earth, Milkyway Galaxy, Known Universe
  39.  
  40.